#ifndef _EiC
#include "cv.h"
#include "highgui.h"
#endif
#include <iostream>
#include <fstream>
#include <math.h>
#include <unistd.h>
#include "matchSide.h"
#include "compare.h"
#include "our_sobel.h"

using namespace std;

int main(int argc, char* argv[]) {
    //cout << "hello world" << endl;
    bool debug = false;
    string probeImageName = "";
    string galleryImageName = "";
    // Process and store the command line inputs
    while(1) {
        int c = getopt(argc,argv,"dp:g:");
        if(c == -1) break;
        switch(c) {
            case 'd': debug = true; break;
            case 'p': probeImageName = optarg; break;
            case 'g': galleryImageName = optarg; break;
        }
    }
    //check inputs
    if(probeImageName == "" || galleryImageName == "") {
        cout << "Usage: " << argv[0] << " -p probeImage -g galleryImage [-d] " << endl;
        return 1;
    }
    //load images
    IplImage *probeSrc = cvLoadImage(probeImageName.c_str(),0);
    IplImage *gallerySrc = cvLoadImage(galleryImageName.c_str(),0);
    
//     IplImage *probeImage = cvLoadImage(probeImageName.c_str(),0);
//     IplImage *galleryImage = cvLoadImage(galleryImageName.c_str(),0);
//     
//     IplImage *probeDir = cvLoadImage(galleryImageName.c_str(),0);
//     IplImage *galleryDir = cvLoadImage(galleryImageName.c_str(),0);
//     
//     IplImage *probeMag = cvLoadImage(galleryImageName.c_str(),0);
//     IplImage *galleryMag = cvLoadImage(galleryImageName.c_str(),0);
    

    //show results
    if (debug)
    {
      cvNamedWindow("Probe Image - original",CV_WINDOW_AUTOSIZE);
      cvShowImage("Probe Image - original",probeSrc);
      cvNamedWindow("Gallery Image - original",CV_WINDOW_AUTOSIZE);
      cvShowImage("Gallery Image - original",gallerySrc);
      cvWaitKey(0);
    }
    
//     int p = normalizeIMG(probeSrc,probeSrc,0);
//     int g = normalizeIMG(gallerySrc,gallerySrc,0);
    
    if (debug)
    {
      cvNamedWindow("Probe Image - norm",CV_WINDOW_AUTOSIZE);
      cvShowImage("Probe Image - norm",probeSrc);
      cvNamedWindow("Gallery Image - norm",CV_WINDOW_AUTOSIZE);
      cvShowImage("Gallery Image - norm",gallerySrc);
      cvWaitKey(0);
    }
//     int p = smoothDiff(probeSrc,probeSrc,3,0);
//     int g = smoothDiff(gallerySrc,gallerySrc,3,0);
// 
//     if (debug) 
//     {    
//       cvNamedWindow("Probe Image - smoothDiff",CV_WINDOW_AUTOSIZE);
//       cvShowImage("Probe Image - smoothDiff",probeImage);
//       cvNamedWindow("Gallery Image - smoothDiff",CV_WINDOW_AUTOSIZE);
//       cvShowImage("Gallery Image - smoothDiff",galleryImage);
//       cvWaitKey(0);
//     }
    //show results
    
//     our_sobel(probeSrc, probeMag, probeDir, 2, 5, debug);
//     our_sobel(gallerySrc, galleryMag, galleryDir, 2, 5, debug);
    probeSrc = haarwavelet(probeSrc);
    gallerySrc = haarwavelet(gallerySrc);
    normalizeIMG(probeSrc,probeSrc);
    normalizeIMG(gallerySrc,gallerySrc);
//     float orig = compareImg(probeImage, galleryImage, debug);
//     float dir = compareImg(probeDir, galleryDir, debug);
    float mag = compareImg(probeSrc, gallerySrc, debug);    
    printf("%9.0f, ", mag);
    
    //printf("\n");
    //cvCanny( probeMag, probeSrc, 7.00, 0.00, 3 );
    //cvCanny( galleryMag, gallerySrc, 7.00, 0.00, 3 );
    //float candir = compareImg(gallerySrc, probeSrc, debug);
    
    //cout << "SSD: " << n << endl;
    return 0;
}
